/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package controller

import (
	"did-service/common/http"

	"github.com/emicklei/go-restful"
)

// DidResolve DID解析
func (dc *DidController) DidResolve(req *restful.Request, resp *restful.Response) {
	did := req.PathParameter("did")

	versionId := req.QueryParameter("versionId")
	if did == "" || versionId == "" {
		log.Error("did or versionId all empty!")
		http.Failed(resp, "请求参数不合法")
		return
	}
	resolve, err := dc.srv.GetResolve(did, versionId)
	if err != nil {
		log.Errorf("查询失败: %v", err)
		http.Failed(resp, err.Error())
		return
	}
	http.OkWithData(resp, "查询成功", resolve)
}

// DidDereference DID引用解析
func (dc *DidController) DidDereference(req *restful.Request, resp *restful.Response) {
	did := req.PathParameter("didURL")
	if did == "" {
		log.Error("did all empty!")
		http.Failed(resp, "请求参数不合法")
		return
	}
	dereference, resource, serviceEndpointUrl, err := dc.srv.GetDereference(did)
	if err != nil {
		log.Errorf("查询失败: %v", err)
		http.Failed(resp, err.Error())
		return
	}
	if !dereference.IsEmpty() {
		http.OkWithData(resp, "查询成功", dereference)
	}

	if !resource.IsEmpty() {
		http.OkWithData(resp, "查询成功", resource)
	}

	if !serviceEndpointUrl.IsEmpty() {
		http.OkWithData(resp, "查询成功", serviceEndpointUrl)
	}
}
